//
//  CZStatusretweetedView.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/16.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit

class CZStatusretweetedView: UIView {

    var retweetedStatus :CZStatusModel? {
    
    
        didSet{
            let retweetedName = retweetedStatus?.user?.screen_name ?? "没有名字"
            
            let retweetedText = retweetedStatus?.text ?? "没有内容"
            
            //设置lab内容
        retweetedLable.text = "@\(retweetedName):\(retweetedText)"
            //设置图片
            
            retweetedPictureView.pictureURLs = retweetedStatus?.storePicutreURLs
            
        // 如果没有内容修改约束
            retweetedPictureView.snp_updateConstraints { (make) in
                let margin = retweetedStatus?.storePicutreURLs == nil ? 0 : HMStatusCellMargin
                make.top.equalTo(retweetedLable.snp_bottom).offset(margin)
                
            }
        
        }
    
    
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        setUI()
        
    }
    
    func setUI(){
    
        //添加子控件
        addSubview(bkgButton)
        addSubview(retweetedLable)
        addSubview(retweetedPictureView)
        
        //设置约束

        // 背景按钮
        bkgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        // 被转发的微博文本内容
        retweetedLable.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(HMStatusCellMargin)
            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        // 配图
        retweetedPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(HMStatusCellMargin)
            make.top.equalTo(retweetedLable.snp_bottom).offset(HMStatusCellMargin)
            
            // 配图大小当别人给他设置pictureURL后他会自动更新,不用我们操心
            make.size.equalTo(CGSizeZero)
        }
        
        // 添加底部约束让被转发的view的底部参照被转发的配图视图
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(retweetedPictureView).offset(HMStatusCellMargin)
        }

    
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private lazy var bkgButton: UIButton = {
    
        let Btn = UIButton()
        Btn.setBackgroundImage(UIImage.init(named: "retweet_background"), forState: .Normal)
        Btn.setBackgroundImage(UIImage.init(named: "retweet_background_highlighted"), forState: .Highlighted)
        
        
        
        
        return Btn
        
    
    
    }()
    private lazy var retweetedLable: UILabel = {
    
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(14)
        lab.textColor = UIColor.darkGrayColor()
        lab.numberOfLines = 0
    
        return lab
    }()
    
    
    private lazy var retweetedPictureView: CZStatusPictureView = CZStatusPictureView()
}
